/*
 * @Author: your name
 * @Date: 2020-09-16 15:27:37
 * @LastEditTime: 2021-01-26 16:52:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense_demo\assets\scripts\roadmap_scene\upgrade_config.ts
 */


import { UG } from "../modules/ugame"
const {ccclass, property} = cc._decorator;

@ccclass
export default class UpgradeConfig extends cc.Component {

    // 节点
    @property(cc.Node)
    public upgrade_root: cc.Node = null;
    @property(cc.Label)
    public last_star_label: cc.Label = null;
    @property(cc.Node)
    public skill_1_items: Array<cc.Node> = [];
    @property(cc.Node)
    public skill_2_items: Array<cc.Node> = [];
    @property(cc.Node)
    public skill_3_items: Array<cc.Node> = [];
    @property(cc.Node)
    public skill_4_items: Array<cc.Node> = [];
    @property(cc.Node)
    public skill_5_items: Array<cc.Node> = [];
    @property(cc.Node)
    public skill_6_items: Array<cc.Node> = [];

    @property(cc.Animation)
    public upgrade_anim: cc.Animation = null;

    // 
    public out_side: boolean = false;
    public skill_config_arr: Array<Array<cc.Node>> = [];    // 二维数组，存放每个技能的节点
    
    onLoad () {
        this.skill_config_arr.push(this.skill_1_items);    
        this.skill_config_arr.push(this.skill_2_items);    
        this.skill_config_arr.push(this.skill_3_items);    
        this.skill_config_arr.push(this.skill_4_items);    
        this.skill_config_arr.push(this.skill_5_items);    
        this.skill_config_arr.push(this.skill_6_items);    

        // 标注技能消耗

        // 显示技能加点界面
        let user_data = UG.get_cur_user();
        this._show_skill_upgrade_config(user_data.skill_level_info, user_data.star_num);
    }

    // 技能升级点击事件
    public on_skill_item_click(e, str: string): void {
        if (this.out_side) {
            return;
        }
        // str ---> 1-5     技能索引-技能等级
        let _arr: Array<string> = str.split("-");
        let skill_index: number = parseInt(_arr[0]);    // 技能索引
        let skill_level: number = parseInt(_arr[1]);    // 技能等级

        // 保存技能等级
        let user_data = UG.get_cur_user();
        user_data.skill_level_info[skill_index - 1] = skill_level;

        // 根据配置表计算
        this._show_skill_upgrade_config(user_data.skill_level_info, user_data.star_num);
    }    

    // 根据技能升级配置表，计算星星
    private _show_skill_upgrade_config(skill_level_info: Array<number>, star_num: number): void {
        let last_star: number = star_num;
    
        // 技能标注加点消耗
        for (let a in UG.tower_skills_upgrade_config) {
            let _node_arr: Array<cc.Node> = this.skill_config_arr[a];
            for (let b = 0; b < _node_arr.length; b++) {
                let _star_label: cc.Label =  _node_arr[b].getChildByName("star_str").getComponent(cc.Label);
                _star_label.string = UG.tower_skills_upgrade_config[a][b + 1].toString();
            }
        }

        // 统计升级技能后剩余的星星
        let i: number = 0;
        let j: number = 0;
        let lv: number = 0;
        for (i = 0; i < skill_level_info.length; i++) {
            lv = skill_level_info[i];
            for (j = 0; j <= lv; j++) {
                last_star -= UG.tower_skills_upgrade_config[i][j];
            }
        }

        // 更新剩余的星星数
        this.last_star_label.string = last_star.toString();

        // 更新技能显示状态
        // skill_level_info: [0, 0, 0, 0, 0, 0],  [skill0, skill1, skill2, skill, skill2, skill]
        for (i = 0; i < skill_level_info.length; i++) {
            lv = skill_level_info[i];
            let _items_arr: Array<cc.Node> = this.skill_config_arr[i];  // 拿到该技能所有节点

            for (j = 0; j < lv; j++) {  // 修改已升级技能显示
                this._set_skill_state(_items_arr[j], 2);
            }

            // 将下一个可升级的技能调整为可升级状态 同时 星星数要足够
            if (j < 5 && last_star >= UG.tower_skills_upgrade_config[i][lv + 1]) {
                this._set_skill_state(_items_arr[j], 1);
                j++;
            }

            // 将剩下的技能状态置灰
            for (; j < 5; j++) {
                this._set_skill_state(_items_arr[j], 0);
            }
        }
    }

    // 切换技能状态
    // 0 表示置灰； 1，表示可以点击升级； 2 表示已经升级，不能点击也不置灰，同时隐藏掉金币数目
    private _set_skill_state(item: cc.Node, state: number): void {
        let item_btn: cc.Button = item.getComponent(cc.Button);
        let skill_sprite: cc.Sprite = item.getComponent(cc.Sprite);
        let star_bg_node: cc.Node = item.getChildByName("star_bg");
        let star_bg_sprite: cc.Sprite = star_bg_node.getComponent(cc.Sprite);
        let star_label_node: cc.Node = item.getChildByName("star_str");

        if (state === 0) {  // 置灰
            item_btn.interactable = false;
            item_btn.enableAutoGrayEffect = true;

            // 置灰
            skill_sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-gray-sprite'));            
            star_bg_sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-gray-sprite'));

            // 显示
            star_bg_node.active = false;
            star_label_node.active = false;
        } else if (state === 1) {   // 可以点击升级
            item_btn.interactable = true;

            // 正常颜色
            skill_sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-sprite'));            
            star_bg_sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-sprite'));

            // 显示
            star_bg_node.active = true;
            star_label_node.active = true;
        } else if (state === 2) {
            item_btn.interactable = false;
            item_btn.enableAutoGrayEffect = false;

            // 隐藏
            star_bg_node.active = false;
            star_label_node.active = false;
        }
    }

    // 完成按钮点击事件
    public on_done_click(e, c): void {
        if (this.out_side) {
            cc.log("====")
            return;
        }
        this.out_side = true;

        // 保存数据
        UG.sync_user_data();

        // 将技能升级界面隐藏
        this.upgrade_anim.play("reverse_road_skill_upgrade_config");
        this.scheduleOnce(()=>{
            this.node.active = false;
            this.out_side = false;
        }, this.upgrade_anim.currentClip.duration);
    }

    // 重置按钮点击事件
    public on_reset_click(e, c): void {
        if (this.out_side) {
            return;
        }
        
        // 还原技能加点
        let user_data = UG.get_cur_user();
        user_data.skill_level_info = [0, 0, 0, 0, 0, 0];

        // 刷新显示
        this._show_skill_upgrade_config(user_data.skill_level_info, user_data.star_num);
    }

    // update (dt) {}
}
